package com.atghost.minio.util.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.slf4j.MDC;
import java.io.Serializable;

@Data
public class RespResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private T data;
    private String code;
    private String message;
    private String txID;

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private String signature;

    public RespResult() {
        this.code = ComErrorCode.SUCCESS.getCode();
        this.message = ComErrorCode.SUCCESS.getMessage();
        this.txID = MDC.get(ApiConsts.THREAD_ID);
    }

    public RespResult(ComErrorCode comErrorCode) {
        this.code = comErrorCode.getCode();
        this.message = comErrorCode.getMessage();
        this.txID = MDC.get(ApiConsts.THREAD_ID);
    }

    public RespResult(T data, ComErrorCode comErrorCode) {
        this.code = comErrorCode.getCode();
        this.message = comErrorCode.getMessage();
        this.txID = MDC.get(ApiConsts.THREAD_ID);
        this.data = data;
    }

    public static RespResult ok() {
        return new RespResult(null, ComErrorCode.SUCCESS);
    }

    public static RespResult ok(Object data) {
        return new RespResult(data, ComErrorCode.SUCCESS);
    }

    public static RespResult error() {
        return new RespResult(null, ComErrorCode.FAILURE);
    }

    public static RespResult error(String message) {
        return error(ComErrorCode.FAILURE.getCode(), message);
    }

    public static RespResult error(String code, String message) {
        RespResult err = new RespResult();
        err.setCode(code);
        err.setMessage(message);
        return err;
    }

    public static <T> RespResult<T> error(String code, String message, T data) {
        RespResult err = new RespResult();
        err.setCode(code);
        err.setMessage(message);
        err.setData(data);
        return err;
    }

    public static RespResult error(ComErrorCode comErrorCode) {
        return new RespResult(comErrorCode);
    }
}
